import 'package:flame/components.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/page/mahjong/util/user_location.dart';

import '../common/mahjong_context.dart';

class MessageUtil {
  static drawMessage(Canvas canvas, String text,
      {Vector2? start, double? fontSize, Direction? direction}) {
    /**
     * 构建文字部分
     */

    /* TextComponent damageText =
        TextComponent(textRenderer: TextPaint(style: _critDamageTextStyle));
    damageText.text = "搞快点嘛";
    damageText.y=11222222221.0;
    damageText.render(canvas);*/
    if (start == null) {
      start = Vector2(0, 0);
    }
    if (fontSize == null) {
      fontSize = 16.sp;
    }

    var textStyle = TextStyle(
      color: Colors.black,
      fontSize: fontSize,
    );
    var textSpan = TextSpan(text: text, style: textStyle);
    var textPainter = TextPainter(
      text: textSpan,
      textDirection: TextDirection.ltr,
    );
    textPainter.layout();
    start.y -= textPainter.height;
    var paint = Paint()
      ..color = Colors.white
      ..style = PaintingStyle.fill;
    // ..maskFilter = MaskFilter.blur(BlurStyle.normal, 1.0)
    //..blendMode = BlendMode.srcATop;

    if (direction == Direction.RIGHT) {
      start.y = -textPainter.height;
      start.x = start.x - textPainter.width - MahjongContext().headImageHeight;
    } else if (direction == Direction.TOP) {
      start.y = MahjongContext().headImageHeight;
      start.x = start.x;
    }
    var path = Path();
    var radius = 3.0;
    /**
     * 构建气泡部分
     */
    double height = textPainter.height;
    double width = textPainter.width + radius * 2;
    path.moveTo(start.x + radius, start.y);

    if (direction == Direction.TOP) {
      path.lineTo(start.x + radius + 10, start.y);
      path.lineTo(start.x + radius + 9, start.y - height / 4);
      path.lineTo(start.x + radius + 20, start.y);
    }

    path.lineTo(start.x + width - radius, start.y);
    path.arcToPoint(Offset(start.x + width, start.y + radius),
        radius: Radius.circular(radius));
    path.lineTo(start.x + width, start.y + height - radius);
    path.arcToPoint(Offset(start.x + width - radius, start.y + height),
        radius: Radius.circular(radius));
    //右下角结束
    // 收尾点
    if (direction == Direction.LEFT || direction == Direction.BOTTOM) {
      path.lineTo(start.x + radius + 10, start.y + height);
      path.lineTo(start.x + radius + 1, start.y + height + height / 4);
      path.lineTo(start.x + radius, start.y + height);
    } else if (direction == Direction.RIGHT) {
      path.lineTo(start.x + width - radius - 10, start.y + height);
      path.lineTo(start.x + width - radius - 9, start.y + height + height / 4);
      path.lineTo(start.x + width - radius - 20, start.y + height);
    }

    path.lineTo(start.x + radius, start.y + height);
    path.arcToPoint(Offset(start.x, start.y + height - radius),
        radius: Radius.circular(radius));
    path.lineTo(start.x, start.y + radius);
    // 收尾
    path.arcToPoint(Offset(start.x + radius, start.y),
        radius: Radius.circular(radius));
    canvas.drawPath(path, paint);
    var x = start.x + radius;
    var y = start.y + (height - textPainter.height) / 2;
    var offset = Offset(x, y);
    textPainter.paint(canvas, offset);
  }
}
